const {src,dest, parallel, series,watch} = require("gulp");
// css
const less = require("gulp-less");
const cleancss = require("gulp-clean-css");
const rename = require("gulp-rename");
//html
const htmlmin = require("gulp-htmlmin")
//del
const del = require("del");
//服务发布
const browserSync = require("browser-sync");
// 创建服务
const bs = browserSync.create();

// 声明style任务
const style = () =>{
    return src("src/css/*.less",{base:"src"})
    .pipe(less())
    .pipe(cleancss())
    .pipe(rename({
        "extname":".min.css"
    }))
    .pipe(dest("dist"))
}

//声明script任务
const script = () =>{
    return src("src/js/**",{base:"src"})
    .pipe(dest("dist"))
}
//声明html任务
const html = () =>{
    return src("src/index.html",{base:"src"})
    .pipe(htmlmin({
        collapseWhitespace: true,
         //minifyJS：用来压缩html中的js代码
         minifyJS:true
    }))
    .pipe(dest("dist"))
}
//声明文件清除任务
const clear = ()=>{
    return del(['dist']);
}
//创建服务发布任务
const serve = () =>{
    // watch("src/index.html",html)
    // watch("src/css/*",style)
    // watch("src/js/**",script)
    bs.init({
        notify:false,
        server:{
            baseDir:"./dist",
        }
    })
}

//组合任务
const build = parallel(style,script,html);

const dev = series(clear,build,serve);

module.exports = {
    build,
    dev,
    serve
}